const path = require("path");
const fs = require("fs");
const {getDate, notice} = require("../utils/util");
const rootPath = require("../utils/rootPath");

let logCreated = false;
const logName = path.join(rootPath, './logs', `LOG${getDate()}.log`);

function init() {
  fs.writeFile(logName, 'init..\n\n', (err) => {
    if (err) {
      console.error('日志创建失败。\n', err);
      process.exit(1);
      return
    }

    notice(`操作日志输出到 ${logName}`);
    logCreated = true;
  })
}

/**
 * 追加信息
 * @param {string} message
 * @return {boolean}
 */
function append(message) {
  if (!logCreated) {
    return false
  }

  fs.appendFileSync(logName, message);
}

module.exports = {
  logName,
  init,
  append,
}
